package com.jayhuang.user_center.utils;

import com.jayhuang.user_center.common.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应消息对象
 *
 * @param <T> 数据泛型
 */
@Data
public class Result<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;
    private String description;

    /**
     * 返回响应对象
     *
     * @param code    状态码
     * @param message 消息
     * @param data    数据
     * @param <T>     数据类型
     * @return 返回对象
     */
    public static <T> Result<T> success(Integer code, String message, T data) {
        Result result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(ErrorCode errorCode) {
        Result result = new Result<>();
        result.setCode(errorCode.getCode());
        result.setMessage(errorCode.getMessage());
        result.setDescription(errorCode.getDescription());
        return result;
    }

    public static <T> Result<T> success(Integer code, String message) {
        return success(code, message, null);
    }

    public static <T> Result<T> error(ErrorCode errorCode) {
        Result result = new Result<>();
        result.setCode(errorCode.getCode());
        result.setMessage(errorCode.getMessage());
        result.setDescription(errorCode.getDescription());
        return result;
    }

    public static <T> Result<T> error(Integer code, String message, T data) {
        Result result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(Integer code, String message) {
        return success(code, message, null);
    }

    public static <T> Result<T> error(Integer code, String message, String description) {
        return success(code, message, null, description);
    }

    private static <T> Result<T> success(Integer code, String message, T data, String description) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        result.setDescription(description);
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode, String message ,String description) {
        Result result = new Result<>();
        result.setCode(errorCode.getCode());
        result.setMessage(message);
        result.setDescription(description);
        return result;
    }

    public static <T> Result<T> error(ErrorCode errorCode ,String description) {
        Result result = new Result<>();
        result.setCode(errorCode.getCode());
        result.setMessage(errorCode.getMessage());
        result.setDescription(description);
        return result;
    }

}
